from flask_jwt_extended import get_jwt_identity
from flask_jwt_extended import jwt_required
from flask import request
from flask import g
from app.common.constants import ResultCode
from app.common.response import api_response


def register_middleware_tenant_check(bp):  # token验证
    @bp.before_request
    @jwt_required()  # 蓝图对象，划分中间件有效
    def get_token():
        user_id = get_jwt_identity()
        if user_id:
            g.user_id = user_id


def user_model(bp):
    @bp.before_request
    def get_data():
        data = request.get_json()
        if data:
            g.data = data
        else:
            return api_response(ResultCode.REQUESTS_STATE)
